from django.urls import path, include
from rest_framework.routers import DefaultRouter
from . import views, views_label, views_workflow, views_api, views_system, views_transfer
from . import views_software
from . import views_analytics_enhanced, views_system_enhanced, views_ai, views_search

app_name = 'assets'

# 为API视图创建路由器
router = DefaultRouter()
router.register(r'assets', views_api.AssetViewSet, basename='asset')
router.register(r'departments', views_api.DepartmentViewSet, basename='department')
router.register(r'locations', views_api.LocationViewSet, basename='location')
router.register(r'suppliers', views_api.SupplierViewSet, basename='supplier')
router.register(r'contracts', views_api.ContractViewSet, basename='contract')
# 注意：软件模块当前仅提供基于模板的CRUD页面，暂无DRF ViewSet

# API URL patterns
api_urlpatterns = [
    path('', include(router.urls)),
]

urlpatterns = [
    path('', views.index, name='index'),
    path('dashboard/', views.dashboard, name='dashboard'),
    # 用户认证
    path('login/', views.user_login, name='login'),
    path('logout/', views.user_logout, name='logout'),
    
    # 软件管理
    path('software/', views_software.software_list, name='software_list'),
    path('software/create/', views_software.software_create, name='software_create'),
    path('software/<int:pk>/', views_software.software_detail, name='software_detail'),
    path('software/<int:pk>/update/', views_software.software_update, name='software_update'),
    path('software/<int:pk>/delete/', views_software.software_delete, name='software_delete'),

    # 软件许可管理
    path('software/licenses/', views_software.license_list, name='license_list'),
    path('software/licenses/create/', views_software.license_create, name='license_create'),
    path('software/licenses/<int:pk>/', views_software.license_detail, name='license_detail'),
    path('software/licenses/<int:pk>/update/', views_software.license_update, name='license_update'),
    path('software/licenses/<int:pk>/delete/', views_software.license_delete, name='license_delete'),

    # 个人资料管理
    path('profile/', views.profile_view, name='profile_view'),
    path('profile/edit/', views.profile_edit, name='profile_edit'),
    path('profile/change-password/', views.change_password, name='change_password'),
    
    # 用户管理
    path('users/', views.user_list, name='user_list'),
    path('users/create/', views.user_create, name='user_create'),
    path('users/<int:pk>/', views.user_detail, name='user_detail'),
    path('users/<int:pk>/edit/', views.user_edit, name='user_edit'),
    path('users/<int:pk>/delete/', views.user_delete, name='user_delete'),
    path('users/<int:pk>/reset-password/', views.user_reset_password, name='user_reset_password'),
    path('users/<int:pk>/toggle-status/', views.user_toggle_status, name='user_toggle_status'),
    path('users/batch-delete/', views.user_batch_delete, name='user_batch_delete'),
    path('users/bulk-import/', views.bulk_import_users, name='bulk_import_users'),
    
    # 用户组管理
    path('groups/', views.group_list, name='group_list'),
    path('groups/create/', views.group_create, name='group_create'),
    path('groups/<int:pk>/', views.group_detail, name='group_detail'),
    path('groups/<int:pk>/edit/', views.group_edit, name='group_edit'),
    path('groups/<int:pk>/delete/', views.group_delete, name='group_delete'),
    
    # 组织管理
    path('organizations/', views.organization_list, name='organization_list'),
    path('organizations/create/', views.organization_create, name='organization_create'),
    path('organizations/<int:pk>/', views.organization_detail, name='organization_detail'),
    path('organizations/<int:pk>/update/', views.organization_update, name='organization_update'),
    path('organizations/<int:pk>/delete/', views.organization_delete, name='organization_delete'),
    path('organizations/bulk-import/', views.bulk_import_organizations, name='bulk_import_organizations'),
    path('organizations/batch-delete/', views.organization_batch_delete, name='organization_batch_delete'),
    
    # 位置管理
    path('locations/', views.location_list, name='location_list'),
    path('locations/create/', views.location_create, name='location_create'),
    path('locations/<int:pk>/', views.location_detail, name='location_detail'),
    path('locations/<int:pk>/update/', views.location_update, name='location_update'),
    path('locations/<int:pk>/delete/', views.location_delete, name='location_delete'),
    # path('locations/batch-delete/', views.location_batch_delete, name='location_batch_delete'),
    
    # 部门管理
    path('departments/', views.department_list, name='department_list'),
    path('departments/create/', views.department_create, name='department_create'),
    path('departments/<int:pk>/', views.department_detail, name='department_detail'),
    path('departments/<int:pk>/update/', views.department_update, name='department_update'),
    path('departments/<int:pk>/delete/', views.department_delete, name='department_delete'),
    path('departments/bulk-import/', views.bulk_import_departments, name='bulk_import_departments'),
    path('departments/batch-delete/', views.department_batch_delete, name='department_batch_delete'),
    
    # 供应商管理
    path('suppliers/', views.supplier_list, name='supplier_list'),
    path('suppliers/create/', views.supplier_create, name='supplier_create'),
    path('suppliers/<int:pk>/', views.supplier_detail, name='supplier_detail'),
    path('suppliers/<int:pk>/update/', views.supplier_update, name='supplier_update'),
    path('suppliers/<int:pk>/delete/', views.supplier_delete, name='supplier_delete'),
    path('suppliers/bulk-import/', views.supplier_bulk_import, name='supplier_bulk_import'),
    path('suppliers/batch-delete/', views.supplier_batch_delete, name='supplier_batch_delete'),
    path('suppliers/<int:pk>/upload-document/', views.supplier_upload_document, name='supplier_upload_document'),
    
    # 供应商资质文件上传
    path('assets/supplier/qualification-file/upload/', views.supplier_qualification_upload, name='supplier_qualification_upload'),
    path('assets/supplier/qualification-file/<int:file_id>/delete/', views.supplier_qualification_delete, name='supplier_qualification_delete'),
    path('assets/supplier/qualification-file/<int:file_id>/view/', views.supplier_document_view, name='supplier_document_view'),
    
    # 合同管理
    path('contracts/', views.contract_list, name='contract_list'),
    path('contracts/create/', views.contract_create, name='contract_create'),
    path('contracts/bulk-import/', views.contract_bulk_import, name='contract_bulk_import'),
    path('contracts/batch-delete/', views.contract_batch_delete, name='contract_batch_delete'),
    path('contracts/<int:pk>/', views.contract_detail, name='contract_detail'),
    path('contracts/<int:pk>/update/', views.contract_update, name='contract_update'),
    path('contracts/<int:pk>/delete/', views.contract_delete, name='contract_delete'),
    
    # 合同文件上传
    path('contracts/<int:pk>/upload-file/', views.contract_upload_file, name='contract_upload_file'),
    path('contracts/file/<int:file_id>/delete/', views.contract_delete_file, name='contract_delete_file'),
    path('contracts/file/<int:file_id>/view/', views.contract_file_view, name='contract_file_view'),
    # 合同提交审核
    path('contracts/<int:pk>/submit-review/', views.contract_submit_review, name='contract_submit_review'),
    
    # 仓库管理
    path('warehouses/', views.warehouse_list, name='warehouse_list'),
    path('warehouses/create/', views.warehouse_create, name='warehouse_create'),
    path('warehouses/<int:pk>/', views.warehouse_detail, name='warehouse_detail'),
    path('warehouses/<int:pk>/update/', views.warehouse_update, name='warehouse_update'),
    path('warehouses/<int:pk>/delete/', views.warehouse_delete, name='warehouse_delete'),
    path('warehouses/batch-delete/', views.warehouse_batch_delete, name='warehouse_batch_delete'),
    
    # 耗材管理
    path('consumables/', views.consumable_list, name='consumable_list'),
    path('consumables/create/', views.consumable_create, name='consumable_create'),
    path('consumables/<int:pk>/', views.consumable_detail, name='consumable_detail'),
    path('consumables/<int:pk>/update/', views.consumable_update, name='consumable_update'),
    path('consumables/<int:pk>/delete/', views.consumable_delete, name='consumable_delete'),
    path('consumables/bulk-import/', views.bulk_import_consumables, name='bulk_import_consumables'),
    path('consumables/batch-delete/', views.consumable_batch_delete, name='consumable_batch_delete'),
    path('consumables/batch-update-stock/', views.consumable_batch_update_stock, name='consumable_batch_update_stock'),
    path('consumables/export/', views.consumable_export, name='consumable_export'),
    path('consumables/low-stock/', views.consumable_low_stock, name='consumable_low_stock'),
    path('consumables/quick-stock-in/', views.consumable_quick_stock_in, name='consumable_quick_stock_in'),
    path('consumables/quick-stock-out/', views.consumable_quick_stock_out, name='consumable_quick_stock_out'),
    
    # 库存记录
    path('inventory-logs/', views.inventory_log_list, name='inventory_log_list'),
    path('inventory-logs/<int:pk>/detail/', views.inventory_log_detail, name='inventory_log_detail'),
    path('consumables/<int:pk>/records/', views.consumable_records, name='consumable_records'),
    
    # 资产管理
    path('assets/', views.asset_list, name='asset_list'),
    path('assets/create/', views.asset_create, name='asset_create'),
    path('assets/<int:pk>/', views.asset_detail, name='asset_detail'),
    path('assets/<int:pk>/update/', views.asset_update, name='asset_update'),
    path('assets/<int:pk>/delete/', views.asset_delete, name='asset_delete'),
    path('assets/<int:pk>/allocate/', views.asset_allocate, name='asset_allocate'),
    path('assets/<int:pk>/transfer/', views.asset_transfer, name='asset_transfer'),
    path('assets/<int:pk>/maintenance/', views.asset_maintenance, name='asset_maintenance'),
    path('assets/<int:pk>/scrap/', views.asset_scrap, name='asset_scrap'),
    path('assets/<int:pk>/recycle/', views.asset_recycle, name='asset_recycle'),
    path('assets/bulk-import/', views.bulk_import_assets, name='bulk_import_assets'),
    path('assets/batch-delete/', views.asset_batch_delete, name='asset_batch_delete'),
    path('assets/batch-update-status/', views.asset_batch_update_status, name='asset_batch_update_status'),
    path('assets/batch-recycle/', views.asset_batch_recycle, name='asset_batch_recycle'),
    
    # 资产类别管理
    path('categories/', views.category_list, name='category_list'),
    path('categories/create/', views.category_create, name='category_create'),
    path('categories/<int:pk>/update/', views.category_update, name='category_update'),
    path('categories/<int:pk>/delete/', views.category_delete, name='category_delete'),
    path('categories/bulk-import/', views.bulk_import_categories, name='bulk_import_categories'),
    path('categories/batch-delete/', views.category_batch_delete, name='category_batch_delete'),
    
    # 模板下载
    path('templates/download/<str:template_type>/', views.download_import_template, name='download_import_template'),
    
    # 维修管理
    path('maintenance/', views.maintenance_list, name='maintenance_list'),
    path('maintenance/create/', views.maintenance_create, name='maintenance_create'),
    path('maintenance/<int:pk>/', views.maintenance_detail, name='maintenance_detail'),
    path('maintenance/<int:pk>/update/', views.maintenance_update, name='maintenance_update'),
    
    # 资产折旧管理
    path('depreciation/', views.asset_depreciation_report, name='depreciation_report'),
    path('depreciation/schedule/<int:asset_id>/', views.asset_depreciation_detail, name='depreciation_schedule'),
    path('depreciation/batch-update/', views.batch_update_depreciation, name='batch_depreciation_update'),
    
    # 资产盘点管理
    path('inventory/plans/', views.inventory_plan_list, name='inventory_plan_list'),
    path('inventory/plans/create/', views.inventory_plan_create, name='inventory_plan_create'),
    path('inventory/plans/<int:pk>/', views.inventory_plan_detail, name='inventory_plan_detail'),
    path('inventory/plans/<int:pk>/start/', views.inventory_plan_start, name='inventory_plan_start'),
    path('inventory/items/<int:pk>/check/', views.inventory_item_check, name='inventory_item_check'),
    
    # 资产借用归还管理
    path('borrow/', views.borrow_request_list, name='borrow_request_list'),
    path('borrow/create/', views.borrow_request_create, name='borrow_request_create'),
    path('borrow/<int:pk>/', views.borrow_request_detail, name='borrow_request_detail'),
    path('borrow/<int:pk>/approve/', views.borrow_request_approve, name='borrow_request_approve'),
    path('borrow/<int:pk>/borrow-out/', views.borrow_asset_out, name='borrow_asset_out'),
    path('borrow/<int:pk>/return/', views.return_asset, name='return_asset'),
    path('borrow/my-requests/', views.my_borrow_requests, name='my_borrow_requests'),
    path('borrow/overdue/', views.overdue_borrows, name='overdue_borrows'),
    
    # 库存预警管理
    path('inventory-alerts/', views.inventory_alert_dashboard, name='inventory_alert_dashboard'),
    path('inventory-alerts/list/', views.inventory_alert_list, name='inventory_alert_list'),
    path('inventory-alerts/<int:pk>/', views.inventory_alert_detail, name='inventory_alert_detail'),
    path('inventory-alerts/<int:pk>/acknowledge/', views.inventory_alert_acknowledge, name='inventory_alert_acknowledge'),
    path('inventory-alerts/<int:pk>/resolve/', views.inventory_alert_resolve, name='inventory_alert_resolve'),
    path('inventory-alerts/<int:pk>/ignore/', views.inventory_alert_ignore, name='inventory_alert_ignore'),
    path('inventory-alerts/batch-action/', views.inventory_alert_batch_action, name='inventory_alert_batch_action'),
    path('inventory-alerts/generate/', views.generate_inventory_alerts, name='generate_inventory_alerts'),
    path('inventory-status-overview/', views.inventory_status_overview, name='inventory_status_overview'),
    
    # 报表与分析模块
    path('analytics/', views.analytics_dashboard, name='analytics_dashboard'),
    path('analytics/assets/', views.asset_analytics, name='asset_analytics'),
    path('analytics/maintenance/', views.maintenance_analytics, name='maintenance_analytics'),
    path('analytics/inventory/', views.inventory_analytics, name='inventory_analytics'),
    path('analytics/export/', views.export_analytics_data, name='export_analytics_data'),
    
    # 增强的分析功能
    path('analytics/advanced/', views_analytics_enhanced.advanced_analytics_dashboard, name='advanced_analytics_dashboard'),
    path('analytics/custom-report-builder/', views_analytics_enhanced.custom_report_builder, name='custom_report_builder'),
    path('analytics/real-time/', views_analytics_enhanced.real_time_dashboard, name='real_time_dashboard'),
    path('analytics/export-advanced-report/', views_analytics_enhanced.export_advanced_report, name='export_advanced_report'),
    
    # 报表管理
    path('reports/', views.report_list, name='report_list'),
    path('reports/assets/', views.asset_report, name='asset_report'),
    path('reports/assets/export/excel/', views.export_asset_report_excel, name='export_asset_report_excel'),
    path('reports/assets/export/pdf/', views.export_asset_report_pdf, name='export_asset_report_pdf'),
    
    # 资产管理增强功能
    path('asset/<int:asset_id>/qr-code/', views.asset_qr_code, name='asset_qr_code'),
    path('asset/<int:asset_id>/qr-code/view/', views.asset_qr_code_view, name='asset_qr_code_view'),
    path('asset/scan-qr/', views.asset_scan_qr, name='asset_scan_qr'),
    path('asset/<int:asset_id>/upload-image/', views.asset_upload_image, name='asset_upload_image'),
    path('asset/<int:asset_id>/upload-images/', views.asset_upload_images, name='asset_upload_images'),
    path('asset/<int:asset_id>/delete-image/', views.asset_delete_image, name='asset_delete_image'),
    path('asset/<int:asset_id>/batch-delete-images/', views.asset_batch_delete_images, name='asset_batch_delete_images'),
    path('asset/<int:asset_id>/set-primary-image/', views.asset_set_primary_image, name='asset_set_primary_image'),
    path('asset/<int:asset_id>/edit-image/', views.asset_edit_image, name='asset_edit_image'),
    path('asset/<int:asset_id>/upload-file/', views.asset_upload_file, name='asset_upload_file'),
    path('asset/<int:asset_id>/delete-file/', views.asset_delete_file, name='asset_delete_file'),
    path('asset/<int:asset_id>/edit-file/', views.asset_edit_file, name='asset_edit_file'),
    path('asset/<int:asset_id>/file/<int:file_id>/view/', views.asset_file_view, name='asset_file_view'),
    path('asset/<int:asset_id>/print-label/', views.asset_print_label, name='asset_print_label'),
    path('asset/<int:asset_id>/print-label-config/', views.asset_print_label_config, name='asset_print_label_config'),
    path('asset/batch-print-labels/', views.asset_batch_print_labels, name='asset_batch_print_labels'),
    path('asset/batch-print-labels-config/', views.asset_print_label_config, name='asset_batch_print_labels_config'),
    
    # 标签模板管理
    path('label-templates/', views_label.label_templates, name='label_template_list'),
    path('label-templates/designer/', views_label.label_template_designer, name='label_template_designer'),
    path('label-templates/save/', views_label.save_label_template, name='save_label_template'),
    path('label-templates/<int:template_id>/delete/', views_label.delete_label_template, name='delete_label_template'),
    path('label-templates/<int:template_id>/duplicate/', views_label.duplicate_label_template, name='duplicate_label_template'),
    path('label-templates/export/', views_label.export_label_templates, name='export_label_templates'),
    path('label-preview/', views_label.preview_label, name='preview_label'),
    path('assets/labels/preview/', views_label.preview_label, name='asset_label_preview'),
    path('label-print/', views_label.print_labels_page, name='print_labels_page'),
    path('label-print/create-job/', views_label.create_print_job, name='create_print_job'),
    path('label-print/job-status/<int:job_id>/', views_label.print_job_status, name='print_job_status'),
    path('label-print-history/', views_label.print_history, name='label_print_history'),
    path('printer-configs/', views_label.printer_configurations, name='printer_configurations'),
    path('printer-configs/save/', views_label.save_printer_config, name='save_printer_config'),
    path('printer-configs/test/<int:config_id>/', views_label.test_printer_connection, name='test_printer_connection'),
    path('asset-selector-data/', views_label.get_asset_selector_data, name='get_asset_selector_data'),
    
    # 资产调拨管理
    path('transfer/', views_transfer.transfer_list, name='transfer_list'),
    path('transfer/create/', views_transfer.transfer_create, name='transfer_create'),
    path('transfer/<int:pk>/', views_transfer.transfer_detail, name='transfer_detail'),
    path('transfer/<int:pk>/submit/', views_transfer.transfer_submit, name='transfer_submit'),
    path('transfer/<int:pk>/approve/', views_transfer.transfer_approve, name='transfer_approve'),
    path('transfer/<int:pk>/execute/', views_transfer.transfer_execute, name='transfer_execute'),
    path('transfer/my-requests/', views_transfer.my_transfer_requests, name='my_transfer_requests'),
    path('transfer/pending-approvals/', views_transfer.pending_approvals, name='pending_approvals'),
    path('transfer/analytics/', views_transfer.transfer_analytics, name='transfer_analytics'),

    # 工作流管理
    path('workflow/tasks/', views_workflow.task_list, name='task_list'),
    path('workflow/tasks/<int:task_id>/process/', views_workflow.process_task, name='process_task'),
    path('workflow/borrow/create/', views_workflow.asset_borrow_request_create, name='asset_borrow_request_create'),
    path('workflow/templates/', views_workflow.workflow_template_list, name='workflow_template_list'),
    path('workflow/templates/create/', views_workflow.workflow_template_create, name='workflow_template_create'),
    path('workflow/templates/<int:pk>/edit/', views_workflow.workflow_template_edit, name='workflow_template_edit'),
    path('workflow/templates/<int:pk>/delete/', views_workflow.workflow_template_delete, name='workflow_template_delete'),
    path('workflow/scrap/<int:asset_id>/create/', views_workflow.asset_scrap_request_create, name='asset_scrap_request_create'),
    
    # 系统管理功能
    path('system/logs/', views_system.system_log_list, name='system_log_list'),
    path('system/asset-logs/', views.asset_log_list, name='asset_log_list'),
    path('system/asset-logs/<int:pk>/', views.asset_log_detail, name='asset_log_detail'),
    path('system/asset-logs/<int:pk>/delete/', views_system.asset_log_delete, name='asset_log_delete'),
    path('system/asset-logs/batch-delete/', views_system.asset_log_batch_delete, name='asset_log_batch_delete'),
    path('system/parameters/', views_system.system_parameter_list, name='system_parameter_list'),
    path('system/parameters/<int:pk>/edit/', views_system.system_parameter_edit, name='system_parameter_edit'),
    path('system/notifications/', views_system.notification_list, name='notification_list'),
    path('system/notifications/<int:pk>/read/', views_system.notification_mark_read, name='notification_mark_read'),
    path('system/notifications/mark-all-read/', views_system.notification_mark_all_read, name='notification_mark_all_read'),
    path('system/backups/', views_system.data_backup_list, name='data_backup_list'),
    path('system/backups/create/', views_system.create_backup, name='create_backup'),
    
    # 增强的系统管理功能
    path('system/dashboard/', views_system_enhanced.system_dashboard, name='system_dashboard'),
    path('system/performance/', views_system_enhanced.system_performance_monitor, name='system_performance_monitor'),
    path('system/maintenance/', views_system_enhanced.system_maintenance, name='system_maintenance'),
    path('system/ajax-system-status/', views_system_enhanced.ajax_system_status, name='ajax_system_status'),
    path('system/ajax-performance-data/', views_system_enhanced.ajax_performance_data, name='ajax_performance_data'),
    
    # 移动端页面
    path('mobile/scan/', views.mobile_asset_scan, name='mobile_asset_scan'),
    
    # API接口 (v1 - function based)
    path('api/asset-search/', views_api.asset_search_api, name='asset_search_api'),
    path('api/assets/', views_api.asset_list_api, name='asset_list_api'),
    path('api/assets/<int:asset_id>/', views_api.asset_detail_api, name='asset_detail_api'),
    path('api/notifications/', views_api.my_notifications_api, name='my_notifications_api'),
    path('api/notifications/<int:notification_id>/read/', views_api.mark_notification_read_api, name='mark_notification_read_api'),
    path('api/borrows/', views_api.my_borrows_api, name='my_borrows_api'),
    path('api/categories/', views_api.categories_api, name='categories_api'),
    path('api/departments/', views_api.departments_api, name='departments_api'),
    path('api/sync/', views_api.sync_offline_data_api, name='sync_offline_data_api'),

    # AI智能化功能
    path('ai/', views_ai.ai_dashboard, name='ai_dashboard'),
    path('ai/recommendations/', views_ai.recommendations_list, name='ai_recommendations_list'),
    path('ai/recommendations/generate/', views_ai.generate_recommendations, name='ai_generate_recommendations'),
    path('ai/recommendations/<int:recommendation_id>/handle/', views_ai.handle_recommendation, name='ai_handle_recommendation'),
    path('ai/anomalies/', views_ai.anomaly_detection_list, name='ai_anomaly_detection_list'),
    path('ai/anomalies/run/', views_ai.run_anomaly_detection, name='ai_run_anomaly_detection'),
    path('ai/anomalies/<int:anomaly_id>/handle/', views_ai.handle_anomaly, name='ai_handle_anomaly'),
    path('ai/predictive-maintenance/', views_ai.predictive_maintenance, name='ai_predictive_maintenance'),
    path('ai/assets/<int:asset_id>/prediction/', views_ai.asset_prediction_detail, name='ai_asset_prediction_detail'),
    path('ai/analytics/', views_ai.ai_analytics, name='ai_analytics'),
    
    # AI API接口
    path('api/ai/recommendations/', views_ai.api_recommendations, name='api_ai_recommendations'),
    path('api/ai/anomalies/', views_ai.api_anomalies, name='api_ai_anomalies'),

    # 高级搜索功能
    path('search/advanced/', views_search.advanced_search, name='advanced_search'),
    path('search/execute/', views_search.execute_search, name='execute_search'),
    path('search/model-fields/<str:model_name>/', views_search.get_model_fields, name='get_model_fields'),
    path('search/save-template/', views_search.save_search_template, name='save_search_template'),
    path('search/templates/', views_search.search_templates, name='search_templates'),
    path('search/export/', views_search.export_search_results, name='export_search_results'),

    # API接口 (v2 - DRF ViewSets)
    path('api/v2/', include(api_urlpatterns)),
]